package com.maila.xiaoaobusiness.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.maila.xiaoaobusiness.R;
import com.maila.xiaoaobusiness.adapter.ClassSortAdapter;
import com.maila.xiaoaobusiness.entry.GoodsKind;
import com.maila.xiaoaobusiness.widget.CanDragListView;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 分类排序
 * Created by mark on 2017/12/1.
 */

public class ClassSortActivity extends AppCompatActivity implements CanDragListView.OnChanageListener{
    @BindView(R.id.white_return_iv)
    AutoRelativeLayout whiteReturnIv;
    @BindView(R.id.drag_lv)
    CanDragListView dragLv;
    ClassSortAdapter classSortAdapter;
    List<GoodsKind> mdata=new ArrayList<>();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_class_sort);
        ButterKnife.bind(this);
        classSortAdapter=new ClassSortAdapter(this);
        dragLv.setAdapter(classSortAdapter);
        dragLv.setOnChangeListener(this);
        initdata();
    }

    private void initdata() {
        mdata.add(new GoodsKind("热卖",0));
        mdata.add(new GoodsKind("打折促销",1));
        mdata.add(new GoodsKind("精美礼盒",2));
        mdata.add(new GoodsKind("实惠组合",3));
        mdata.add(new GoodsKind("自选拼盘",4));
        mdata.add(new GoodsKind("限时抢购",5));
        classSortAdapter.setMdata(mdata);
    }

    @OnClick({R.id.white_return_iv, R.id.tv_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.white_return_iv:
                finish();
                break;
            case R.id.tv_save:
                break;
        }
    }

    @Override
    public void onChange(int start, int to) {
        if(start < to){
            for(int i=start; i<to; i++){
                Collections.swap(mdata, i, i+1);
            }
        }else if(start > to){
            for(int i=start; i>to; i--){
                Collections.swap(mdata, i, i-1);
            }
        }
        classSortAdapter.notifyDataSetChanged();
    }
}
